/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package contaspagar.bd;

import conexao.ConexaoSingleton;
import contaspagar.classes.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Gabi
 */
public class ContasPagarDAO {

    private EntityManager manager;
    private Query query;

    public ContasPagarDAO() throws Exception {
        manager = ConexaoSingleton.getConexao();
    }

    public List<ContasPagar> getContasVencendo(String dataDoDia) {
        query = manager.createNativeQuery("select * from contapagar where dataPagamento = "+"'"+dataDoDia+"'"+" order by cedente");
        return query.getResultList();
    }

    public List<ContasPagar> getContasVencer(String dataDoDia) {
        query = manager.createNativeQuery("select * from contapagar where dataPagamento > "+"'"+dataDoDia+"'"+" order by cedente");
        return query.getResultList();
    }

    public List<ContasPagar> getContasVencidas(String dataDoDia) {
        query = manager.createNativeQuery("select * from contapagar where dataPagamento < "+"'"+dataDoDia+"'"+" order by cedente");
        return query.getResultList();
    }

    public void salvar(ContasPagar contaPagar) {
        manager.getTransaction().begin();
        manager.persist(contaPagar);
        manager.getTransaction().commit();
    }

    public void excluir(ContasPagar contaPagar) {
        manager.getTransaction().begin();
        manager.remove(contaPagar);
        manager.getTransaction().commit();
    }

//    public List<ContasPagar> pesquisaCedenteVencendo(String cedente, Date dataDoDia) {
//        query = manager.createQuery("select cp from contapagar where cp.nome like "+cedente+" and cp.datapagamento = "+"'"+dataDoDia+"'");
//        return query.getResultList();
//    }
//
//    public List<ContasPagar> pesquisaCedenteVencer(String cedente, Date dataDoDia) {
//        query = manager.createQuery("select cp from contapagar where cp.nome like "+cedente+" and cp.datapagamento > "+"'"+dataDoDia+"'");
//        return query.getResultList();
//    }
//
//    public List<ContasPagar> pesquisaCedenteVencidos(String cedente, Date dataDoDia) {
//        query = manager.createQuery("select cp from contapagar where cp.nome like "+cedente+" and cp.datapagamento < "+"'"+dataDoDia+"'");
//        return query.getResultList();
//    }

    public String getDataDoDia() throws ParseException{
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        String data = df.format(new Date (System.currentTimeMillis()));
        return data;
    }
}
